package com.hbjycl.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;

import org.neo4j.cypher.internal.compiler.v2_1.ast.rewriters.addUniquenessPredicates;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import scala.annotation.meta.getter;

import com.hbjycl.entity.Tree;
import com.hbjycl.service.TreeService;



/**
 * Handles requests for the application home page.
 */
@Controller
public class CoursewareController extends BaseController
{
    
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(CoursewareController.class);

    @Resource
    private TreeService treeservice;
    /**
     * 前台首页
     * 
     * @param session
     * @return
     */
    @RequestMapping(value = "/coursewareview", method = RequestMethod.GET)
    public ModelAndView index(HttpSession session, HttpServletRequest request)
    {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("courseware/courseware");
        return mav;
        }
    /*
     * 教师讲义树
     */
    @RequestMapping(value = "/lecturestree")
    public void coursetree(HttpSession session, HttpServletRequest request,HttpServletResponse response) throws IOException{
  List<Tree> treelist =treeservice.getAll();//查询出的list  
   String jsonStr  = JSONArray.fromObject(treelist).toString();//转换成json字符串  

       response.setCharacterEncoding("utf8");  
       PrintWriter out  = response.getWriter();  
       out.println(jsonStr);//输出到前台  
   } 
    /*
     * 课件的具体内容
     */
    @RequestMapping(value = "/coursedata", method = RequestMethod.GET)
    public ModelAndView courseware(HttpSession session, HttpServletRequest request)
    {
    	String nodeid=request.getParameter("nodeid");
        ModelAndView mav = new ModelAndView();
        mav.setViewName("courseware/coursedata/"+nodeid);
        return mav;
        }
    /*
     * 讲义的具体内容
     */
    @RequestMapping(value = "/lecturesdata", method = RequestMethod.GET)
    public ModelAndView lecturesdata(HttpSession session, HttpServletRequest request)
    {
        ModelAndView mav = new ModelAndView();
        String nodeid=request.getParameter("nodeid");
        Tree tree=treeservice.getTreeByTid(nodeid);
        
        mav.addObject("title", tree.getTitle());
        mav.addObject("code", tree.getCode());
        mav.addObject("content", tree.getContent());
        mav.setViewName("courseware/lecturesdata");
        return mav;
        }
     /*
      * 课件列表查询
      */
     @RequestMapping(value = "/courselist", method = RequestMethod.GET)
     public ModelAndView courselist( 
    		 @RequestParam(value = "tid", required = true, defaultValue = "") String tid,
             @RequestParam(value = "name", required = true, defaultValue = "") String name,HttpSession session, HttpServletRequest request)
     {  	 
         ModelAndView mav = new ModelAndView();
         List<Tree> treelist =treeservice.getAll();
         List<Tree> findlist =new ArrayList<Tree>();
         if (name.trim().length() > 0)
         {
        	 for(int i=0;i<treelist.size();i++)
        	 {
        		 if(name.equals(treelist.get(i).getName()))
        			 findlist.add(treelist.get(i));
        	 }
         } 
         else
         {
        	 findlist =treeservice.getAll();
         }
         mav.addObject("treelist",findlist);
         mav.addObject("name",name);
         mav.setViewName("manage/course/courselist");
         return mav;
         }
     
     /*
      * 课件添加页面
      */
     @RequestMapping(value = "/courseaddview", method = RequestMethod.GET)
     public ModelAndView courseaddview(HttpSession session, HttpServletRequest request)
     {
         ModelAndView mav = new ModelAndView();
         List<Tree> treelist =treeservice.getAll();//查询出的list 
         mav.addObject("treelist",treelist);
         mav.setViewName("manage/course/courseadd");
         return mav;
         }
     /*
      * 课件添加
      */
     @RequestMapping(value = "/courseadd", method = RequestMethod.POST)
     public ModelAndView courseadd(
    	       @RequestParam(value = "tid", required = true, defaultValue = "") String tid,
               @RequestParam(value = "pid", required = false, defaultValue = "0") String pid,
               @RequestParam(value = "name", required = true, defaultValue = "") String name,
               @RequestParam(value = "title", required = false, defaultValue = "") String title,
               @RequestParam(value = "code", required = false) String code,
               @RequestParam(value = "content", required = false) String content,
    		 HttpSession session, HttpServletRequest request){
         ModelAndView mav = new ModelAndView();
         mav.setViewName("manage/course/courselist");
         Tree tree=new Tree();
         tree.setTid(tid);
         if(treeservice.isExistByTid(tid))
         {
        	 mav.addObject(TIP_ERROR, "此id已存在");
        	 mav.setViewName("manage/course/courseadd");
         }
         else
         {      
         tree.setPid(pid);
         tree.setName(name);
         tree.setTitle(title);
         tree.setCode(code);
         tree.setContent(content);
         Tree pnametree=treeservice.getTreeByTid(tree.getPid());
         tree.setPname(pnametree.getName());
         try{
             treeservice.save(tree);
         }
         catch(Exception e)
         {
        	 mav.addObject(TIP_ERROR, e);
        	 mav.setViewName("manage/course/courseadd");
         }
         List<Tree> treelist =treeservice.getAll();//查询出的list 
         mav.addObject("treelist",treelist);
         }
         return mav;
         }
     /*
      * 编辑课件页面
      */
     @RequestMapping(value = "/courseeditview", method = RequestMethod.GET)
     public ModelAndView courseeditview(HttpSession session, HttpServletRequest request)
     {
         ModelAndView mav = new ModelAndView();
         mav.setViewName("manage/course/courseedit");
         String id=request.getParameter("id");
         Tree tree=treeservice.get(id);
         mav.addObject("tree", tree);
         List<Tree> treelist =treeservice.getAll();//查询出的list 
         mav.addObject("treelist",treelist);
         return mav;
         }
     /*
      * 编辑课件
      */
     @RequestMapping(value = "/courseedit", method = RequestMethod.POST)
     public ModelAndView courseedit(
    	       @RequestParam(value = "tid", required = true, defaultValue = "") String tid,
               @RequestParam(value = "pid", required = false, defaultValue = "0") String pid,
               @RequestParam(value = "name", required = true, defaultValue = "") String name,
               @RequestParam(value = "title", required = false, defaultValue = "") String title,
               @RequestParam(value = "code", required = false) String code,
               @RequestParam(value = "content", required = false) String content,
    		 HttpSession session, HttpServletRequest request){
         ModelAndView mav = new ModelAndView();
         Tree tree=new Tree();
         tree=treeservice.getTreeByTid(tid);
         tree.setTid(tid);
         tree.setPid(pid);
         Tree pnametree=treeservice.getTreeByTid(tree.getPid());
         tree.setPname(pnametree.getName());
         tree.setName(name);
         tree.setTitle(title);
         tree.setCode(code);
         tree.setContent(content);
         try{
             treeservice.update(tree);
         }
         catch(Exception e)
         {
        	 mav.addObject(TIP_ERROR, e);
        	 mav.setViewName("manage/course/courseupdate");
         }
         List<Tree> treelist =treeservice.getAll();//查询出的list 
         mav.addObject("treelist",treelist);
         mav.setViewName("manage/course/courselist");
         return mav;
         }
     /*
      * 编辑课件
      */
     @RequestMapping(value = "/coursedelete", method = RequestMethod.GET)
     public ModelAndView coursedelete(HttpSession session, HttpServletRequest request)
     {
         ModelAndView mav = new ModelAndView();
         mav.setViewName("manage/course/courseedit");
         String id=request.getParameter("id");
         Tree tree=treeservice.get(id);
         mav.addObject("tree", tree);
         List<Tree> treelist =treeservice.getAll();//查询出的list 
         mav.addObject("treelist",treelist);
         return mav;
         }
     /*
      * 教师讲义子节点页面
      * 
      */
     @RequestMapping(value = "/childnodeview", method = RequestMethod.GET)
     public ModelAndView childnodeview(HttpSession session, HttpServletRequest request)
     {
         ModelAndView mav = new ModelAndView();
         mav.setViewName("manage/course/courseedit");
         String id=request.getParameter("id");
         Tree tree=treeservice.get(id);
         mav.addObject("tree", tree);
         return mav;
         }
}
